﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Widgetsphere.Generator.Common.GeneratorFramework;

namespace Widgetsphere.Generator.Models
{
	public static class Extensions
	{
		#region GetAllChildRelations

		public static List<Relation> GetAllChildRelations(this List<Table> tableList)
		{
			List<Relation> retval = new List<Relation>();
			foreach (Table table in tableList)
			{
				foreach (Relation relation in table.ChildRoleRelations)
				{
					retval.Add(relation);
				}
			}
			return retval;
		}

		#endregion

		#region CorePropertiesHash

		//public static string GetCorePropertiesHash(this ColumnCollection list)
		//{
		//  List<Column> newList = new List<Column>();
		//  newList.AddRange(list);
		//  return GetCorePropertiesHash(newList);
		//}

		public static string GetCorePropertiesHash(this IEnumerable<Column> list)
		{
			List<ColumnBase> newList = new List<ColumnBase>();
			newList.AddRange(list.ToArray());
			return GetCorePropertiesHash(newList);
		}

		public static string GetCorePropertiesHash(this IEnumerable<CustomViewColumn> list)
		{
			List<ColumnBase> newList = new List<ColumnBase>();
			newList.AddRange(list.ToArray());
			return GetCorePropertiesHash(newList);
		}

		public static string GetCorePropertiesHash(this IEnumerable<CustomStoredProcedureColumn> list)
		{
			List<ColumnBase> newList = new List<ColumnBase>();
			newList.AddRange(list.ToArray());
			return GetCorePropertiesHash(newList);
		}

		public static string GetCorePropertiesHash(this IEnumerable<ColumnBase> list)
		{
			SortedDictionary<string, ColumnBase> sortedList = new SortedDictionary<string, ColumnBase>();
			foreach (ColumnBase c in list)
			{
				sortedList.Add(c.Name + "-" + c.Key, c);
			}

			string hash = "";
			foreach (string key in sortedList.Keys)
			{
				ColumnBase c = sortedList[key];
				hash += c.CorePropertiesHash;
			}
			return hash;
		}

		//public static string GetCorePropertiesHash(this CustomViewColumnCollection list)
		//{
		//  List<CustomViewColumn> newList = new List<CustomViewColumn>();
		//  newList.AddRange(list);
		//  return GetCorePropertiesHash(newList);
		//}

		//public static string GetCorePropertiesHash(this IEnumerable<CustomViewColumn> list)
		//{
		//  SortedDictionary<string, CustomViewColumn> sortedList = new SortedDictionary<string, CustomViewColumn>();
		//  foreach (CustomViewColumn c in list)
		//  {
		//    sortedList.Add(c.Name + "-" + c.Key, c);
		//  }

		//  string hash = "";
		//  foreach (string key in sortedList.Keys)
		//  {
		//    Column c = sortedList[key];
		//    hash += c.CorePropertiesHash;
		//  }
		//  return hash;
		//}

		#endregion

		#region Windows Controls

		public static void RemoveRange<T>(this List<T> list, IEnumerable<T> removeItems)
			 where T : IModelObject
		{
			foreach (T o in removeItems)
			{
				list.Remove(o);
			}
		}

		public static T[] ToArray<T>(this CheckedListBox.ObjectCollection list)
			 where T : IModelObject
		{
			List<T> retval = new List<T>();
			foreach (T o in list)
			{
				retval.Add(o);
			}
			return retval.ToArray();
		}

		public static T[] ToArray<T>(this CheckedListBox.CheckedItemCollection list)
			 where T : IModelObject
		{
			List<T> retval = new List<T>();
			foreach (T o in list)
			{
				retval.Add(o);
			}
			return retval.ToArray();
		}

		public static TreeNode[] ToArray(this TreeNodeCollection list)
		{
			List<TreeNode> retval = new List<TreeNode>();
			foreach (TreeNode n in list)
			{
				retval.Add(n);
			}
			return retval.ToArray();
		}

		public static void SelectAll(this ListView ctrl)
		{
			foreach (ListViewItem item in ctrl.Items)
			{
				item.Selected = true;
			}
		}

		public static void UnselectAll(this ListView ctrl)
		{
			foreach (ListViewItem item in ctrl.Items)
			{
				item.Selected = false;
			}
		}

		public static void SelectAll(this CheckedListBox ctrl)
		{
			for (int ii = 0; ii < ctrl.Items.Count; ii++)
			{
				ctrl.SetItemChecked(ii, true);
			}
		}

		public static void UnselectAll(this CheckedListBox ctrl)
		{
			for (int ii = 0; ii < ctrl.Items.Count; ii++)
			{
				ctrl.SetItemChecked(ii, false);
			}
		}

		public static IEnumerable<ListViewItem> SelectedList(this ListView ctrl)
		{
			List<ListViewItem> retval = new List<ListViewItem>();
			foreach (ListViewItem item in ctrl.SelectedItems)
			{
				retval.Add(item);
			}
			return retval;
		}

		#endregion

		#region IEnumerable IndexOf

		public static int IndexOf<T>(this IEnumerable<T> obj, T value)
		{
			return obj
					.Select((a, i) => (a.Equals(value)) ? i : -1)
					.Max();
		}

		public static int IndexOf<T>(this IEnumerable<T> obj, T value
					 , IEqualityComparer<T> comparer)
		{
			return obj
					.Select((a, i) => (comparer.Equals(a, value)) ? i : -1)
					.Max();
		}

		#endregion

		#region Menus

		public static void AddMenuItem(this List<MenuCommand> menuList, string text)
		{
			AddMenuItem(menuList, text, null);
		}

		public static void AddMenuItem(this List<MenuCommand> menuList, string text, EventHandler handler)
		{
			DefaultMenuCommand newMenu = new DefaultMenuCommand();
			newMenu.Text = text;
			if (handler != null) newMenu.Click += handler;
			menuList.Add(newMenu);
		}

		#endregion

	}
}